package com.ruoyi.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.utils.AccessKeyUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ParkingAccessKeysMapper;
import com.ruoyi.domain.ParkingAccessKeys;
import com.ruoyi.service.IParkingAccessKeysService;

import javax.annotation.Resource;

/**
 * 访问密钥Service业务层处理
 * 
 * @author dongdong
 * @date 2025-08-21
 */
@Service
public class ParkingAccessKeysServiceImpl implements IParkingAccessKeysService 
{
    @Value("${server.host}")
    private String serverHost;

    @Value("${server.port}")
    private int serverPort;

    @Resource
    private ParkingAccessKeysMapper parkingAccessKeysMapper;

    /**
     * 查询访问密钥
     * 
     * @param id 访问密钥主键
     * @return 访问密钥
     */
    @Override
    public ParkingAccessKeys selectParkingAccessKeysById(Long id)
    {
        return parkingAccessKeysMapper.selectParkingAccessKeysById(id);
    }

    /**
     * 查询访问密钥列表
     * 
     * @param parkingAccessKeys 访问密钥
     * @return 访问密钥
     */
    @Override
    public List<ParkingAccessKeys> selectParkingAccessKeysList(ParkingAccessKeys parkingAccessKeys)
    {
        return parkingAccessKeysMapper.selectParkingAccessKeysList(parkingAccessKeys);
    }

    /**
     * 新增访问密钥
     * 
     * @param parkingAccessKeys 访问密钥
     * @return 结果
     */
    @Override
    public int insertParkingAccessKeys(ParkingAccessKeys parkingAccessKeys)
    {
        parkingAccessKeys.setAccessKeyId(AccessKeyUtil.generateAccessKeyId());
        parkingAccessKeys.setAccessSecret(AccessKeyUtil.generateAccessSecret());
        String platformId = AccessKeyUtil.generatePlatformId();
        parkingAccessKeys.setPlatformId(platformId);
        parkingAccessKeys.setCreateTime(DateUtils.getNowDate());
        return parkingAccessKeysMapper.insertParkingAccessKeys(parkingAccessKeys);
    }

    /**
     * 修改访问密钥
     * 
     * @param parkingAccessKeys 访问密钥
     * @return 结果
     */
    @Override
    public int updateParkingAccessKeys(ParkingAccessKeys parkingAccessKeys)
    {
        parkingAccessKeys.setUpdateTime(DateUtils.getNowDate());
        return parkingAccessKeysMapper.updateParkingAccessKeys(parkingAccessKeys);
    }

    /**
     * 批量删除访问密钥
     * 
     * @param ids 需要删除的访问密钥主键
     * @return 结果
     */
    @Override
    public int deleteParkingAccessKeysByIds(Long[] ids)
    {
        return parkingAccessKeysMapper.deleteParkingAccessKeysByIds(ids);
    }

    /**
     * 删除访问密钥信息
     * 
     * @param id 访问密钥主键
     * @return 结果
     */
    @Override
    public int deleteParkingAccessKeysById(Long id)
    {
        return parkingAccessKeysMapper.deleteParkingAccessKeysById(id);
    }

    @Override
    public ParkingAccessKeys createAccessKeys() {
        ParkingAccessKeys parkingAccessKeys = new ParkingAccessKeys();
        parkingAccessKeys.setAccessKeyId(AccessKeyUtil.generateAccessKeyId());
        parkingAccessKeys.setAccessSecret(AccessKeyUtil.generateAccessSecret());
        parkingAccessKeys.setPlatformId(AccessKeyUtil.generatePlatformId());
        parkingAccessKeys.setCreateTime(DateUtils.getNowDate());
        parkingAccessKeysMapper.insertParkingAccessKeys(parkingAccessKeys);
        return parkingAccessKeys;
    }

    @Override
    public ParkingAccessKeys getAccessKeysByPlatformId(String platformId) {
        ParkingAccessKeys parkingAccessKeys = parkingAccessKeysMapper.selectParkingAccessKeysByPlatformId(platformId);
        return parkingAccessKeys;
    }

    @Override
    public int updateAccessKeysByPlatformId(String platformId) {
        ParkingAccessKeys parkingAccessKeys = parkingAccessKeysMapper.selectParkingAccessKeysByPlatformId(platformId);
        String wssUrl = String.format("wss://%s/websocket/platform/"+platformId, serverHost);
        parkingAccessKeys.setWssUrl(wssUrl);
        return parkingAccessKeysMapper.updateParkingAccessKeys(parkingAccessKeys);
    }

    @Override
    public List<Map<String, Object>> selectList() {
        return parkingAccessKeysMapper.selectList();
    }

}
